package com.tywh;

import com.tywh.pojo.EntiyTest;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;

import static com.tywh.constant.Constant.*;
import static com.tywh.utils.GetToken.GetAccesstoken;

/**
 * @ClassName TestDemoPost
 * @Description post方式调用
 * @Author xhx
 * @Date 2019/7/29 10:57
 * @Version 1.0
 */
public class TestDemoPost {

    public static void main(String[] args) {
        //sdl地址
//        String wsdl = "http://192.168.88.204:8090/api/TscmService.asmx/SpdTrans";
        String wsdl = WSDL + "/" + METHOD;
        //请求参数
//        String appid = "18042802944202";
//        String appsecret = "3609978a-b789-6e62-042e-51015092c77d";
//        String ifname = "RecHosCstList";
//        String nonce = "tongyuwuhan";
        String timestamp = String.valueOf(System.currentTimeMillis());

        String accesstoken = GetAccesstoken(APPID, timestamp, NONCE, APPSECRET);
        //请求参数拼接到地址后
        wsdl = wsdl + "?appid=" + APPID + "&ifname=" + IFNAME + "&timestamp=" + timestamp + "&nonce=" + NONCE + "&accesstoken=" + accesstoken;
        //请求数据
        List<EntiyTest> entiyTestList = new ArrayList<EntiyTest>();
        EntiyTest entiyTest = new EntiyTest("1","123","22","国药武汉");
        entiyTestList.add(entiyTest);
        String data = com.alibaba.fastjson.JSON.toJSONString(entiyTestList);
        System.out.println("请求参数：" + data);
        //发送POST请求，返回为JSON字符串
        String sendPost = sendPost(data, wsdl);
        System.out.println("返回数据：" + sendPost);
    }

    public static String sendPost(String data,String url) {
        boolean isSuccess = false;
        String str="";
        HttpPost post = null;

        try {
            HttpClient httpClient = new DefaultHttpClient();
            // 设置超时时间
            httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 6000);
            httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 6000);

            post = new HttpPost(url);

            // 构造消息头 Content-type应与对方文档一致  application/x-www-form-urlencoded
            post.setHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");

            // 构建消息实体
//            StringEntity entity = new StringEntity(data, Charset.forName("UTF-8"));
            //创建参数列表
            List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
            list.add(new BasicNameValuePair("request", data));
            //url格式编码
            UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(list,"UTF-8");
            //设置数据编码格式
            uefEntity.setContentEncoding("UTF-8");
            // 发送Json格式的数据请求 ContentType应与对方文档一致  application/x-www-form-urlencoded
            uefEntity.setContentType("application/x-www-form-urlencoded");
            post.setEntity(uefEntity);
            //发送HTTPPOST请求
            HttpResponse response = httpClient.execute(post);
            // 检验返回码
            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println("返回码：" + statusCode);
            if(statusCode != HttpStatus.SC_OK){
                isSuccess = false;
            }else{
                int retCode = 0;
                /**读取服务器返回过来的json字符串数据**/
                str = EntityUtils.toString(response.getEntity());
            }

        } catch (Exception e) {
            e.printStackTrace();
            isSuccess = false;
        }finally{
            if(post != null){
                try {
                    post.releaseConnection();
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        return str;

    }

}
